package com.rsscollector.controller.admin.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.ee.servlet.QuartzInitializerServlet;

public class ExecuteJobAction extends Action {

	public static Logger logger = Logger.getLogger(ExecuteJobAction.class
			.getName());

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// Input
		String jobName = request.getParameter("jobName");
		String jobGroup = request.getParameter("jobGroup");
		
		// Get Scheduler object 
		ServletContext context = this.getServlet().getServletContext();
		SchedulerFactory schedulerFactory = (SchedulerFactory) context
				.getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY);
		Scheduler scheduler = schedulerFactory.getScheduler();
		
		// Trigger this job
		scheduler.triggerJob(jobName, jobGroup);
		
		return mapping.findForward("success");
	}

}
